<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array('Html', 'Form');

	var $scaffold;

	function beforeFilter() {

		parent::beforeFilter();
		$this->Auth->allow('register', 'registerSuccess', 'registerConfirm', 'logout', 'login', 'menu');
	}

	function login() {

		if($this->Auth->login() === true){
			if (!empty($this->data)) {
				if (empty($this->data['User']['remember_me'])) {
					$this->Cookie->del('User');
				} else {
					$cookie = array();
					$cookie['email'] = $this->data['User']['email'];
					$cookie['token'] = $this->data['User']['password'];
					$this->Cookie->write('User', $cookie, true, '+2 weeks');
				}
				unset($this->data['User']['remember_me']);
			}

			$path = $this->Auth->redirect();
			$this->redirect($path);
		}
		else {
			// $this->referer(); (= walking_tours)
		}
	}

	function logout(){ // NOTE: if you use debug-messages for cookie then they are not deleted at once!

		$this->Cookie->del('User');
		$this->Session->destroy();
		$this->Auth->logout();
		$path = $this->Auth->redirect();
		$this->redirect($path);
		exit;
	}

	function menu(){

		if(isset($this->authUser)){
			$sUsername = $this->authUser['firstname'].'&nbsp;'.$this->authUser['lastname'];
			$this->set('sUsername', $sUsername);
		}
	}

	function getKey($sName){

	}

	function register() {

		$sKey = $this->Session->read('register_key');
		if(empty($sKey)){
			$sKey = md5(time().rand());
			$this->Session->write('register_key', $sKey);
		}
		$this->set('sKey', $this->Session->read('register_key'));
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$sMail = $this->data['User']['email'];
				$this->Session->write('register_email', $sMail);
				$this->redirect('/controller/index/users/registerSuccess/');
			} else {
			// Validation errors
				$this->set('errorMessage', 'Please correct errors below.');
				$this->render();
			}
		}
	}

	function registerSuccess() {

		$this->set('sKey', $this->Session->read('register_key'));
		$this->set('sEmail', $this->Session->read('register_email'));
		$this->Session->delete('register_key');
		$this->Session->delete('register_email');
		/*
		$message = "Line 1\nLine 2\nLine 3";
		$message = wordwrap($message, 70);
		mail('juergen.riemer@gmail.com', 'My Subject', $message);
		$this->Mailer->Subject = "Nice Subject";
		$this->Mailer->Message = "That was easy";
		$this->Mailer->AddAddress("yourname","juergen.riemer@gmail.com");
		$this->Mailer->socketmail();
		*/
	}

	function registerConfirm($sEmail, $sKey) {

		$oUser = $this->User->findByEmail($sEmail);
		$bValid = ($oUser['User']['register_key'] == $sKey);
		if($bValid){
			$this->data = array('User' => array());
			$this->data['User']['id'] = $oUser['User']['id'];
//			$this->data['User']['confirmed'] = '1';
//			$this->data['User']['register_key'] = '-invalidated-';
			$this->User->save($this->data);
			$this->Auth->login($oUser);
		}
		$this->set('sFirstname', $oUser['User']['firstname']);
		$this->set('sLastname', $oUser['User']['lastname']);
		$this->set('uriProceed', '/cake/controller/index/');
		$this->set('bValid', $bValid);
	}

}



/* *****************
 *
 * function dispcccccccccccclay() {
		$sFilter = " User.id = '9' ";
		//		$sFilter = null;
		$joinRecords = $this->User->test->findAll(array('test.user_id' => 9));
		debug($joinRecords);
		$ids = Set::extract($joinRecords, '{n}.test.event_date_id');
		debug($ids);
		$this->set('aData', $this->User->findAll($sFilter));

	}

function editUser($id = null){

	if(empty($this->data)){
		$this->set('data', $this->User->test->findAll());
	}
	else{
		if($this->User->save($this->data)){
			$this->flash('passt', 'asdf', 2);
		}
	}
	 
}

*/


?>

